<?php
namespace app\kit;

class Common
{
    static public function cgi() {
        $request = \Yii::$app->getRequest();
        return array_merge($request->get(),$request->post());
    }
    static function array_recursion(&$data, $callback) {
        if (is_array($data)) {
            foreach ($data as $key => &$value) {
                if (is_array($value)) {
                    self::array_recursion($value, $callback);
                } else {
                    $value = $callback($value);
                }
            }
        }
        return $data;
    }

    /**
     * 递归处理int和float为字符串
     */
    static function array_recursion_string(&$data) {
        if (is_array($data)) {
            foreach ($data as $key => &$value) {
                if (is_array($value)) {
                    $value = self::array_recursion_string($value);
                } elseif (is_int($value) || is_float($value)) {
                    $value = strval($value);
                }
            }
        }
        return $data;
    }

    static function array_recursion_fields(&$data, $fields, $callback) {
        if (is_array($data)) {
            foreach ($data as $key => &$value) {
                if (is_array($value)) {
                    self::array_recursion_fields($value, $fields, $callback);
                } elseif (is_string($key) && in_array($key, $fields) && !empty($value)) {
                    $value = $callback($value);
                }
            }
        }
        //return $data;
    }

    /**
     * 时区转换
     * @param string $value  时间字符串
     * @param string $to 目标时区
     * @param string $from null:会使用当前时区
     * @param string $format
     * @return false|string
     */
    static function time_convert($value, $to  = 'UTC', $from = 'Asia/Shanghai', $format = 'Y-m-d H:i:s')
    {
        $dateTime = new \DateTime($value, new \DateTimeZone($from));
        $dateTime->setTimezone(new \DateTimeZone($to));
        $value = $dateTime->format($format);
        return $value;
    }
}
